/* File Name: server.c */  
#include<stdio.h>  
#include<stdlib.h>  
#include<string.h>  
#include<errno.h>  
#include<sys/types.h>  
#include<sys/socket.h>  
#include<netinet/in.h>  

#include<iostream>
#include<cstdlib>

using namespace std;

//head files of Linux
#include<unistd.h>   //for fork and read

#include<arpa/inet.h>

#include <fcntl.h>  
#include <sys/select.h>  
#include<sys/ioctl.h>  





#define DEFAULT_PORT 8000  
#define MAXLINE 4096

struct send_info
{
    float x;
    float y;
    float z;
    float a;
    float b;
    float c;

    float angel1;
    float angel2;
    float angel3;
    float angel4;
    float angel5;
    float angel6;

};




int main(int argc, char** argv)  
{  
    int    socket_fd, connect_fd;  
    struct sockaddr_in servaddr;
    char    buff[4096];  
    int     n;
    struct send_info packetip,rev_packet;
    //初始化Socket  
    if( (socket_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1 ){  
    printf("create socket error: %s(errno: %d)\n",strerror(errno),errno);  
    exit(0);  
    }  
    //初始化  
    memset(&servaddr, 0, sizeof(servaddr));  
    servaddr.sin_family = AF_INET;  
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);//IP地址设置成INADDR_ANY,让系统自动获取本机的IP地址。
    servaddr.sin_port = htons(5000);//设置的端口为DEFAULT_PORT
  
    //将本地地址绑定到所创建的套接字上  
    if( bind(socket_fd, (struct sockaddr*)&servaddr, sizeof(servaddr)) == -1){  
        printf("bind socket error: %s(errno: %d)\n",strerror(errno),errno);
        exit(0);
    }  
    //开始监听是否有客户端连接  
    if( listen(socket_fd, 10) == -1){  
        printf("listen socket error: %s(errno: %d)\n",strerror(errno),errno);
        exit(0);
    }  
    printf("======waiting for client's request======\n");
    if( (connect_fd = accept(socket_fd, (struct sockaddr*)NULL, NULL)) == -1){
        printf("accept socket error: %s(errno: %d)",strerror(errno),errno);
    }
    while(1){

//        cout<<"send:"<<endl;
//        struct send_info send_pack; //定义结构体变量
//        char snd_buf[1024];
//        send_pack.x=1;
//        send_pack.y=0;
//        send_pack.z=0;
//        send_pack.a=4;
//        send_pack.b=0;
//        send_pack.c=6;
//
//        send_pack.angel1=0;
//        send_pack.angel2=0;
//        send_pack.angel3=0;
//        send_pack.angel4=0;
//        send_pack.angel5=0;
//        send_pack.angel6=10;
//
//
//        memset(snd_buf,0,1024);
//        memcpy(snd_buf,&send_pack,sizeof(send_pack)); //结构体转换成字符串
//
//        int needSend=sizeof(send_pack);
//
//        send(connect_fd,snd_buf,needSend,0);//发送信息
//
//        cout << sizeof(send_pack)<< endl;
//
//        usleep(500000);


        //阻塞直到有客户端连接，不然多浪费CPU资源。
//
        //接受客户端传过来的数据
        n = recv(connect_fd, buff, MAXLINE, 0);

        //buff[n] = '\0';

        memset(&rev_packet,0,sizeof(rev_packet));
        memcpy(&rev_packet,buff,sizeof(rev_packet));


        printf("recv msg from client: %f\n", rev_packet.angel6);
        close(connect_fd);
    }  
    close(socket_fd);  
}  